<?php

namespace app\forms\field;

use yii\db\Exception;
use app\forms\Model;
use app\models\Field;
use yii\web\HttpException;
use app\validators\PositiveInteger;

/**
 * 字段 首页
 */
class IndexForm extends Model
{
    public $module;
    public $pageSize;
    public $page;

    public function rules()
    {
        return [
            ['module', PositiveInteger::class],
            ['page', PositiveInteger::class],
            ['pageSize', PositiveInteger::class],
        ];
    }

    /**
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function index($param)
    {
        $form = new self();
        $form->setAttributes($param);
        if (!$form->validate()) {
            foreach ($form->getErrors() as $key => $val)
                $form->$key = '';
        }
        $attr = $form->getAttributes();
        try {
            return (new Field())->getList($attr);
        } catch (Exception $e) {
            $form->serverError();
        }
    }

}